import { http } from '/@/utils/http';
import type {
  ArticleInfoResultModel,
  ArticleInfoParams,
  ArticleSpColumnModel,
  ArticleSpColumnParams,
  DraftPageParams,
  DraftPageGetResultModel,
  ArticlePageParams,
  ArticlePageGetResultModel,
  AddArticleParams,
  ArticleListParams,
  ArticleListGetResultModel,
  AddCommentParams,
  AddCommentResult,
  AddCommentStarParams,
  AddCommentStarResult,
} from './model/articleModel';
import type {
  ArticleGetResultModel,
  AddArticleGetResultModel,
  AddDraftParams,
  UpdateDraftParams,
} from './model/editorModel';
import { ContentTypeEnum } from '/@/enums/httpEnum';

/**
 * 给文章的评论点赞
 * @param params AddCommentStarParams
 * @returns AddCommentStarResult
 */
export function postCommentStar(params: AddCommentStarParams) {
  return http.post<AddCommentStarResult>({
    url: '/api/web/article/comments/like',
    params,
  });
}

/**
 * 文章评论
 * @param params AddCommentParams
 * @returns AddCommentResult
 */
export function addComment(params: AddCommentParams) {
  return http.post<AddCommentResult>({
    url: '/api/web/article/comments/add',
    params,
  });
}

// 文章详情
export function queryArticleInfo(params: ArticleInfoParams) {
  return http.get<ArticleInfoResultModel>({
    url: '/api/app/article/article/info',
    params,
  });
}

// 专栏
export function queryArticleSpColumnList(params: ArticleSpColumnParams) {
  return http.post<ArticleSpColumnModel>({
    url: '/api/app/article/sp_column/page',
    data: params,
  });
}

/**
 * 删除单篇文章
 * @param id number
 * @returns null
 */
export function deleteArticle(id: number) {
  return http.post({
    url: '/api/web/article/article/delete',
    params: {
      ids: [id],
    },
  });
}

/**
 * 删除多篇文
 * @param ids number[]
 * @returns null
 */
export function deleteArticles(ids: number[]) {
  return http.post({
    url: '/api/web/article/article/delete',
    params: {
      ids,
    },
  });
}

/**
 * 获取全部文章列表
 * @param params ArticlePageParams
 * @returns ArticlePageGetResultModel
 */
export function queryArticleList(params: ArticleListParams) {
  return http.post<ArticleListGetResultModel>({
    url: '/api/web/article/article/list',
    params: {
      ...params,
      type: 1,
    },
  });
}

/**
 * 获取文章全部分页
 * @param params ArticlePageParams
 * @returns ArticlePageGetResultModel
 */
export function queryArticlePage(params: ArticlePageParams) {
  return http.post<ArticlePageGetResultModel>({
    url: '/api/web/article/article/page',
    params: {
      ...params,
      type: 1,
    },
  });
}

/**
 * 获取文章已发布分页
 * @param params ArticlePageParams
 * @returns ArticlePageGetResultModel
 */
export function queryPublishedArticlePage(params: ArticlePageParams) {
  return http.post<ArticlePageGetResultModel>({
    url: '/api/web/article/article/page',
    params: {
      ...params,
      state: 0,
      type: 1,
    },
  });
}

/**
 * 获取文章已下架分页
 * @param params ArticlePageParams
 * @returns ArticlePageGetResultModel
 */
export function queryPublishedButRejectedArticlePage(params: ArticlePageParams) {
  return http.post<ArticlePageGetResultModel>({
    url: '/api/web/article/article/page',
    params: {
      ...params,
      state: 1,
      type: 1,
    },
  });
}

/**
 * 获取草稿分页
 * @param params DraftPageParams
 * @returns DraftPageGetResultModel
 */
export function queryDraftPage(params: DraftPageParams) {
  return http.post<DraftPageGetResultModel>({
    url: '/api/web/article/article/page',
    params: {
      ...params,
      type: 0,
    },
  });
}

/**
 * 读取文章
 * @param id number
 * @returns ArticleGetResultModel
 */
export function queryArticle(id: number) {
  return http.post<ArticleGetResultModel>({
    url: '/api/web/article/article/info',
    params: {
      id,
    },
  });
}

/**
 * 发布文章
 * @param params AddArticleParams
 * @returns AddArticleGetResultModel
 */
export function publishArticle(params: AddArticleParams) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/update',
    headers: {
      'Content-Type': ContentTypeEnum.JSON,
    },
    params: {
      ...params,
      state: 0,
      type: 1,
    },
  });
}

/**
 * 上架文章
 * @param id number
 * @returns AddArticleGetResultModel
 */
export function resolveArticle(id: number) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/update',
    headers: {
      'Content-Type': ContentTypeEnum.JSON,
    },
    params: {
      id,
      state: 0,
    },
  });
}

/**
 * 下架文章
 * @param id number
 * @returns AddArticleGetResultModel
 */
export function rejectArticle(id: number) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/update',
    headers: {
      'Content-Type': ContentTypeEnum.JSON,
    },
    params: {
      id,
      state: 1,
    },
  });
}

/**
 * 新增草稿
 * @param params AddDraftParams
 * @returns AddArticleGetResultModel
 */
export function addDraft(params: AddDraftParams) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/add',
    params: {
      ...params,
      type: 0,
      state: 3,
    },
  });
}

/**
 * 更新未发布文章的草稿
 * @param params UpdateDraftParams
 * @returns AddArticleGetResultModel
 */
export function updateDraft(params: UpdateDraftParams) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/update',
    headers: {
      'Content-Type': ContentTypeEnum.JSON,
    },
    params: {
      ...params,
    },
  });
}

/**
 * 更新已发布文章的草稿
 * @param params UpdateDraftParams
 * @returns AddArticleGetResultModel
 */
export function updatePublishedDraft(params: UpdateDraftParams) {
  return http.post<AddArticleGetResultModel>({
    url: '/api/web/article/article/update',
    headers: {
      'Content-Type': ContentTypeEnum.JSON,
    },
    params: {
      ...params,
    },
  });
}
